package com.meow.action.front;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.meow.dao.entity.visa.Continent;
import com.meow.dao.entity.visa.VisaCountry;
import com.meow.dao.mapper.visa.ContinentDao;
import com.meow.dao.mapper.visa.VisaCountryDao;
import com.meow.util.AjaxOut;
import com.meow.util.ExtLimit;

@Controller
@RequestMapping("/html/visa/FrontContinentAction/")
public class FrontContinentAction {
	private static final long serialVersionUID = 6095370804319200990L;
	
	@Autowired
	private ContinentDao continentDao;
	
	@Autowired
	private BasicDataSource ds;
	
	@Autowired
	private VisaCountryDao visaCountryDao;
	private Logger logger = Logger.getLogger(FrontContinentAction.class);
	
	@RequestMapping("getContinentCount.action")
	public void getContinentCount(HttpServletRequest request, HttpServletResponse response) {
		try {
			JSONObject obj = new JSONObject();
			int count = continentDao.selectLimitCount(null);
			obj.put("count", count);
			AjaxOut.responseText(response, obj.toString());
		}
		catch (Exception e) {
			logger.error("", e);
		}
	}
	
	@RequestMapping("showContinent.action")
	public String showContinent(HttpServletRequest request, HttpServletResponse response) {
		try {
			Connection conn = ds.getConnection();
			try {
				
				String tableName = request.getParameter("tableName");
				String id = request.getParameter("id");
				String sql = "select content from " + tableName + " where id=?";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.setInt(1, Integer.parseInt(id));
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()) {
					String rsContent = rs.getString("content");
					request.setAttribute("content", rsContent);
				}
				rs.close();
				pstmt.close();
			}
			finally {
				conn.close();
			}
			return "/HTML/ContentPreview.jsp";
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return "";
	}
	
	@RequestMapping("showContentNew.action")
	public String showContentNew(HttpServletRequest request, HttpServletResponse response) {
		try {
			Connection conn = ds.getConnection();
			try {
				
				String tableName = request.getParameter("tableName");
				String id = request.getParameter("id");
				String sql = "select content from " + tableName + " where id=?";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.setInt(1, Integer.parseInt(id));
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()) {
					String rsContent = rs.getString("content");
					request.setAttribute("content", rsContent);
				}
				rs.close();
				pstmt.close();
			}
			finally {
				conn.close();
			}
			return "/HTML/ContentPreview_new.jsp";
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return "";
	}
	
	@RequestMapping("showContent.action")
	public String showContent(HttpServletRequest request, HttpServletResponse response) {
		try {
			Connection conn = ds.getConnection();
			try {
				
				String tableName = request.getParameter("tableName");
				String id = request.getParameter("id");
				String sql = "select content from " + tableName + " where id=?";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.setInt(1, Integer.parseInt(id));
				ResultSet rs = pstmt.executeQuery();
				if(rs.next()) {
					String rsContent = rs.getString("content");
					request.setAttribute("content", rsContent);
				}
				rs.close();
				pstmt.close();
			}
			finally {
				conn.close();
			}
			return "/HTML/ContentPreview_new.jsp";
		}
		catch (Exception e) {
			logger.error("", e);
		}
		return "";
	}
	
	@RequestMapping("showContentCountries.action")
	public void showContentCountries(HttpServletRequest request, HttpServletResponse response) {
		try {
			String continentId = request.getParameter("continentId");
			String start = request.getParameter("start");
			String limit = request.getParameter("limit");
			VisaCountry searchEty = new VisaCountry();
			searchEty.setContinentId(Integer.parseInt(continentId));
			ExtLimit extLimit = new ExtLimit();
			extLimit.setLimit(Integer.parseInt(limit));
			extLimit.setStart(Integer.parseInt(start));
			searchEty.setExtLimit(extLimit);
			int count = visaCountryDao.selectLimitCount(searchEty);
			List data = visaCountryDao.selectByLimit(searchEty);
			request.setAttribute("countries", data);
			AjaxOut.responseJSonGrid(response, data, count, new SimpleDateFormat("yyyy-MM-dd"));
		}
		catch (Exception e) {
			logger.error("", e);
		}
	}
	
	@RequestMapping("getAllContentCountries.action")
	public void getAllContentCountries(HttpServletRequest request, HttpServletResponse response) {
		try {
			JSONArray allDataArr = new JSONArray();
			List allContinentData = continentDao.selectByEntity(null);
			for(int i = 0; i < allContinentData.size(); i++) {
				Continent ety = (Continent) allContinentData.get(i);
				
				VisaCountry cEty = new VisaCountry();
				cEty.setContinentId(ety.getId());
				List countryListData = visaCountryDao.selectByEntity(cEty);
				
				JSONObject obj = new JSONObject();
				obj.put("name", ety.getName());
				
				JSONArray arr = new JSONArray();
				arr.addAll(countryListData);
				obj.put("countries", arr);
				
				allDataArr.add(obj);
			}
			AjaxOut.responseJSonGrid(response, allDataArr);
		}
		catch (Exception e) {
			logger.error("", e);
		}
	}
}
